package org.dromara.platform.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.platform.domain.bo.TMiniProgramAuthenticationBo;
import org.dromara.platform.domain.vo.TMiniProgramAuthenticationVo;
import org.dromara.platform.domain.TMiniProgramAuthentication;
import org.dromara.platform.mapper.TMiniProgramAuthenticationMapper;
import org.dromara.platform.service.ITMiniProgramAuthenticationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 小程序认证信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-14
 */
@RequiredArgsConstructor
@Service
public class TMiniProgramAuthenticationServiceImpl implements ITMiniProgramAuthenticationService {

    private final TMiniProgramAuthenticationMapper baseMapper;

    /**
     * 查询小程序认证信息
     *
     * @param id 主键
     * @return 小程序认证信息
     */
    @Override
    public TMiniProgramAuthenticationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询小程序认证信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 小程序认证信息分页列表
     */
    @Override
    public TableDataInfo<TMiniProgramAuthenticationVo> queryPageList(TMiniProgramAuthenticationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TMiniProgramAuthentication> lqw = buildQueryWrapper(bo);
        Page<TMiniProgramAuthenticationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的小程序认证信息列表
     *
     * @param bo 查询条件
     * @return 小程序认证信息列表
     */
    @Override
    public List<TMiniProgramAuthenticationVo> queryList(TMiniProgramAuthenticationBo bo) {
        LambdaQueryWrapper<TMiniProgramAuthentication> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TMiniProgramAuthentication> buildQueryWrapper(TMiniProgramAuthenticationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TMiniProgramAuthentication> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TMiniProgramAuthentication::getId);
        lqw.eq(bo.getBaseId() != null, TMiniProgramAuthentication::getBaseId, bo.getBaseId());
        lqw.eq(bo.getCustomerType() != null, TMiniProgramAuthentication::getCustomerType, bo.getCustomerType());
        lqw.eq(bo.getPayType() != null, TMiniProgramAuthentication::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getQualificationOther()), TMiniProgramAuthentication::getQualificationOther, bo.getQualificationOther());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), TMiniProgramAuthentication::getAccountName, bo.getAccountName());
        lqw.eq(bo.getAccountNameType() != null, TMiniProgramAuthentication::getAccountNameType, bo.getAccountNameType());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountSupplemental()), TMiniProgramAuthentication::getAccountSupplemental, bo.getAccountSupplemental());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthIdentification()), TMiniProgramAuthentication::getAuthIdentification, bo.getAuthIdentification());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthIdentMaterial()), TMiniProgramAuthentication::getAuthIdentMaterial, bo.getAuthIdentMaterial());
        lqw.eq(StringUtils.isNotBlank(bo.getThirdPartyPhone()), TMiniProgramAuthentication::getThirdPartyPhone, bo.getThirdPartyPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceAppid()), TMiniProgramAuthentication::getServiceAppid, bo.getServiceAppid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TMiniProgramAuthentication::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), TMiniProgramAuthentication::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), TMiniProgramAuthentication::getMobile, bo.getMobile());
        lqw.eq(bo.getInvoiceType() != null, TMiniProgramAuthentication::getInvoiceType, bo.getInvoiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxpayerId()), TMiniProgramAuthentication::getTaxpayerId, bo.getTaxpayerId());
        lqw.eq(StringUtils.isNotBlank(bo.getEnterprisePhone()), TMiniProgramAuthentication::getEnterprisePhone, bo.getEnterprisePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEnterpriseAddress()), TMiniProgramAuthentication::getEnterpriseAddress, bo.getEnterpriseAddress());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), TMiniProgramAuthentication::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccount()), TMiniProgramAuthentication::getBankAccount, bo.getBankAccount());
        return lqw;
    }

    /**
     * 新增小程序认证信息
     *
     * @param bo 小程序认证信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TMiniProgramAuthenticationBo bo) {
        TMiniProgramAuthentication add = MapstructUtils.convert(bo, TMiniProgramAuthentication.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改小程序认证信息
     *
     * @param bo 小程序认证信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TMiniProgramAuthenticationBo bo) {
        TMiniProgramAuthentication update = MapstructUtils.convert(bo, TMiniProgramAuthentication.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TMiniProgramAuthentication entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除小程序认证信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
